package com.hb.trade.api.routine.config.pay;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * 微信支付相关参数
 */
public class WeixinPayConfig {
    /**
     * 微信回调的成功返回信息
     */
    public static final String SUCCESS_RESULT = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";

    /**
     * 沙箱环境支付密钥请求地址
     */
    public static final String SAND_BOX_KEY_URL = "https://api.mch.weixin.qq.com/xdc/apiv2getsignkey/sign/getsignkey";

    public static final String TRADE_TYPE = "NATIVE";

    // APP和网页支付提交用户端ip, Native支付填调用微信支付API的机器IP, 即：服务器ip地址
    public static final String SPBILL_CREATE_IP = "127.0.0.1";
    // 微信支付 - 统一下单地址
    public static final String PLACEANORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    // 回调地址
    public static final String NOTIFY_URL = "/hbapi/v1.0/notify/userOrder/weChatCallback";

    public static String getSandBoxKey(String nonceStr, String sign) {
        if (StrUtil.isEmpty(nonceStr)) {
            return null;
        }
        JSONObject obj = JSONUtil.createObj();
        obj.putOpt("mch_id", PaymentConfig.WX_MCH_ID);
        obj.putOpt("nonce_str", nonceStr);
        obj.putOpt("sign", sign);
        String post = HttpUtil.post(SAND_BOX_KEY_URL, obj);
        return post;
    }

    public static String notifyUrl() {
        String publicHost = SpringUtil.getProperty("hb.public-host");
        return publicHost + NOTIFY_URL;
    }
}
